<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.attr" placeholder="属性"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPageConfig">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="configs" highlight-current-row v-loading="listLoading" border max-height="620" style="width: 100%;">

			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="attr" label="属性" width="180" sortable>
			</el-table-column>
			<el-table-column prop="attrValue" label="属性值"   sortable>
			</el-table-column>
			<el-table-column prop="describes" label="属性描述" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button size="small" >删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>
		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="属性" prop="attr">
					<el-input v-model="editForm.attr" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="属性值"  prop="attrValue">
					<el-input v-model="editForm.attrValue"></el-input>
				</el-form-item>
				<el-form-item label="属性描述"  prop="describes">
					<el-input   v-model="editForm.describes"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="属性" prop="attr">
					<el-input v-model="addForm.attr" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="属性值" prop="attrValue">
					<el-input v-model="addForm.attrValue"></el-input>
				</el-form-item>
				<el-form-item label="属性描述" prop="describes">
					<el-input   v-model="addForm.describes"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { findPageConfig ,saveConfig,updateConfig} from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					attr: ''
				},
                start:0,
                pageSize:10,
                total:0,
                loading: false,
				listLoading: false,
                configs: [],
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
                    attr: [
						{ required: true, message: '请输入属性', trigger: 'blur' }
					],attrValue: [
                        { required: true, message: '请输入属性值', trigger: 'blur' }
                    ],describes: [
                        { required: true, message: '请输入属性描述', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {
                    attr: '',
                    attrValue: '',
                    describes: ''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    attr: [
                        { required: true, message: '请输入属性', trigger: 'blur' }
                    ],attrValue: [
                        { required: true, message: '请输入属性值', trigger: 'blur' }
                    ],describes: [
                        { required: true, message: '请输入属性描述', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				addForm: {
                    attr: '',
                    attrValue: '',
                    describes: ''
				}
			}
		},
		methods: {
			handleCurrentChange:function(val){
                this.start = val - 1;
                this.findPageConfig();
            },
            handleSizeChange:function(val){
                this.pageSize = val;
                this.findPageConfig();
            },
            findPageConfig() {
                let para = {
                    start:this.start,
                    pageSize:this.pageSize,
                    attr:this.filters.attr,
					attrValue:'',
					describes:''
                };
                this.loading = true;
                findPageConfig(para).then(datas => {
                    this.loading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.configs = data.result;
                        this.total = data.totalCount;
                    }
                }).catch(e =>{

                });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    attr: '',
                    attrValue: '',
                    describes: ''
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
                            updateConfig(para).then((res) => {
                                this.editLoading = false;
                                let { message, code, data } = res;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                } else {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.findPageConfig();
                                }
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
                            saveConfig(para).then((res) => {
                                this.addLoading = false;
                                let { message, code, data } = res;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                } else {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    this.findPageConfig();
                                }
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			}

		},
		mounted() {
			this.findPageConfig();
		}
	}

</script>

<style scoped>

</style>
